import React from "react";
import {Table} from "antd";

import BaseComponent from "../BaseComponent";

export default class Grid extends BaseComponent {

    static defaultProps = {
        _initLoading: true,//是否初始化显示加载提示
        _onInitLoading: undefined,//初始化加载函数
        _showPage: true,//是否显示分页
        _pageSize: 20,//默认pageSize大小
        _onPageChange: undefined,//分页改变
        dataSource: [],//[] 或 {curPage: 1, pageSize: 20, totalCount: 0, result: []},
        bordered: true,
        _onRowClick: undefined,//({record, index, event})=>{}
        _onRowDoubleClick: undefined,//({record, index, event})=>{}
        _onRowContextMenu: undefined,//({record, index, event})=>{}
        _onRowMouseEnter: undefined,//({record, index, event})=>{}
        _onRowMouseLeave: undefined,//({record, index, event})=>{}
    };

    constructor(props) {
        super(props);
        let {dataSource, _pageSize} = this.props;
        if (_pageSize < 1) {
            _pageSize = 20;
        }
        if (dataSource instanceof Array) {
            this.dataSource = {curPage: 1, pageSize: _pageSize, totalCount: 0, result: dataSource};
        } else if (dataSource && typeof this.props.dataSource === 'object') {
            this.dataSource = {curPage: 1, pageSize: _pageSize, totalCount: 0, result: [], ...dataSource};
        } else {
            this.dataSource = {curPage: 1, pageSize: _pageSize, totalCount: 0, result: []};
        }
        this.state.loading = this.props._initLoading;
    }

    componentDidMount() {
        if (this.props._initLoading) {
            if (this.props._onInitLoading) {
                let {curPage, pageSize, totalCount} = this.dataSource;
                this.props._onInitLoading({curPage, pageSize, totalCount});
            }
        }
    }


    closeLoadingIndicator() {
        this.setState({
            loading: false
        });
    }

    showLoadingIndicator(text = '加载中...') {
        this.setState({
            loading: true,
            loadingTip: text
        })
    }

    getCurPage() {
        return this.dataSource.curPage;
    }

    setData(data, {curPage, pageSize, totalCount} = {}) {
        if (!data instanceof Array) {
            data = [];
        }
        this.dataSource.result = data;
        this._setPageInfo({curPage, pageSize, totalCount});
        this.closeLoadingIndicator();
    }

    getData() {
        return this.dataSource.result;
    }

    getDataAt(index) {
        return this.getData()[index];
    }

    _setPageInfo({curPage, pageSize, totalCount}) {
        if (curPage > 0) {
            this.dataSource.curPage = curPage;
        }
        if (pageSize > 0) {
            this.dataSource.pageSize = pageSize;
        }
        if (totalCount >= 0) {
            this.dataSource.totalCount = totalCount;
        }
    }

    _onPageChange(page, pageSize) {
        if (this.props._onPageChange) {
            this.props._onPageChange({curPage: page, pageSize});
        }
    }

    render() {
        let options = {...this.props};
        let loading = {
            tip: this.state.loadingTip || '加载中...',
            spinning: this.state.loading
        };
        if (options.loading && typeof options.loading === 'object') {
            loading = {
                ...loading,
                ...options.loading,
                spinning: this.state.loading
            }
        }

        let pagination = false;
        if (this.props._showPage) {
            pagination = {
                showTotal: (total, range) => `${range[0]}-${range[1]} 共 ${total} 条数据`,
                total: this.dataSource.totalCount,
                current: this.dataSource.curPage,
                pageSize: this.dataSource.pageSize,
                onChange: this._onPageChange.bind(this)
            };
            if (options.pagination && typeof options.pagination === 'object') {
                pagination = {
                    ...pagination,
                    ...options.pagination,
                    total: this.dataSource.totalCount,
                    current: this.dataSource.curPage,
                    pageSize: this.dataSource.pageSize,
                    onChange: this._onPageChange.bind(this)
                }
            }
        }

        let onRow = options.onRow;
        if (!onRow) {
            onRow = (record, index) => {
                return {
                    onClick: event => {
                        if (this.props._onRowClick) {
                            this.props._onRowClick({record, index, event});
                        }
                    },
                    onDoubleClick: event => {
                        if (this.props._onRowDoubleClick) {
                            this.props._onRowDoubleClick({record, index, event});
                        }
                    },
                    onContextMenu: event => {
                        if (this.props._onRowContextMenu) {
                            this.props._onRowContextMenu({record, index, event});
                        }
                    },
                    onMouseEnter: event => {
                        if (this.props._onRowMouseEnter) {
                            this.props._onRowMouseEnter({record, index, event});
                        }
                    },
                    onMouseLeave: event => {
                        if (this.props._onRowMouseLeave) {
                            this.props._onRowMouseLeave({record, index, event});
                        }
                    }
                };
            }
        }

        delete options.onRow;
        delete options.ref;
        delete options.loading;
        delete options.dataSource;
        delete options.pagination;
        return (
            <Table
                loading={loading}
                pagination={pagination}
                dataSource={this.dataSource.result}
                onRow={onRow}
                {...options}>
                {
                    this.props.children
                }
            </Table>
        )
    }
}
